import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.MongoDatabase;
import okhttp3.*;
import java.io.IOException;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

public class getErrorCompany {
    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("企查查");
    static MongoCollection collection = mDatabase.getCollection("31Error_List");

    public static void main(String[] args) throws IOException {
        /*
         * 要用把excel上传到企查查后 拿到的cookie
         * */

        String Cookie = "QCCSESSID=c2q1mvjhf2kmedkkubvr5tu5o6; UM_distinctid=1791114fc16164-032e14db4fb80a-d7e1739-1fa400-1791114fc1779c; zg_did=%7B%22did%22%3A%20%221791114fc2377f-0e1bc0b7b33479-d7e1739-1fa400-1791114fc24887%22%7D; _uab_collina=161948925864339480527481; zg_5068e513cb8449879f83e2a7142b20a6=%7B%22sid%22%3A%201619495165375%2C%22updated%22%3A%201619495166337%2C%22info%22%3A%201619495166059%2C%22superProperty%22%3A%20%22%7B%5C%22%E5%BA%94%E7%94%A8%E5%90%8D%E7%A7%B0%5C%22%3A%20%5C%22%E6%8B%9B%E6%8A%95%E6%A0%87WEB%E7%AB%AF%5C%22%7D%22%2C%22platform%22%3A%20%22%7B%5C%22js_platform%5C%22%3A%20%5C%22js%5C%22%2C%5C%22client_type%5C%22%3A%20%5C%221%5C%22%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.qcc.com%22%2C%22cuid%22%3A%20%228e7bca95134d78d3aabc28b186d73b54%22%7D; acw_tc=7cefefa816195820765693127ed16d002c151784119d24bc659c130598; CNZZDATA1254842228=1639464656-1619487167-https%253A%252F%252Fwww.baidu.com%252F%7C1619578967; hasShow=1; zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201619582076987%2C%22updated%22%3A%201619582081344%2C%22info%22%3A%201619489258536%2C%22superProperty%22%3A%20%22%7B%5C%22%E5%BA%94%E7%94%A8%E5%90%8D%E7%A7%B0%5C%22%3A%20%5C%22%E4%BC%81%E6%9F%A5%E6%9F%A5%E7%BD%91%E7%AB%99%5C%22%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%5C%22%24utm_source%5C%22%3A%20%5C%22baidu1%5C%22%2C%5C%22%24utm_medium%5C%22%3A%20%5C%22cpc%5C%22%2C%5C%22%24utm_term%5C%22%3A%20%5C%22pzsy%5C%22%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22cuid%22%3A%20%228e7bca95134d78d3aabc28b186d73b54%22%2C%22zs%22%3A%200%2C%22sc%22%3A%200%7D";
        String data = "{page:1}";
        JSONObject jsonObject = JSONObject.parseObject(data);

        String url = "https://www.qcc.com/api/batch/getDetailsByIds";
        int rownum = 1;
        int curPage = 1;
        int count = 0;
        OkHttpClient client = new OkHttpClient();
        while (true){
            FormBody formBody = new FormBody.Builder()
                    .add("page", String.valueOf(curPage))
                    .build();
            Request request = new Request.Builder()
                    .addHeader("Cookie", Cookie)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("origin", "https://www.qcc.com")
                    .addHeader("referer", "https://www.qcc.com/web/project/batchList")
                    .addHeader("5c0f957234e9ac66e9d2", "6cfeb539b531d11747020b0e7ccb4dfc01076af314032a42f540d7354ca01e59cc0b50a032fee9240166b00accdb3a8996173e5c13d232fbd4a12dbb9cc5ea55")
                    .post(formBody).url(url).build();
            Response response = client.newCall(request).execute();
            String resString = response.body().string();
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(resString).get("Result").toString());


            for (int i = 0; i < jsonArray.size(); i++) {
                if (JSONObject.parseObject(jsonArray.get(i).toString()).get("Status").toString().equals("BatchFailed")){
                    Document doc = new Document("企业名称",JSONObject.parseObject(jsonArray.get(i).toString()).get("Name").toString() );
                    collection.insertOne(doc);
                    count++;
                    System.out.println("正在写入------------------------------");
                }else {
                    System.out.println("共计： "+ count);
                    return;
                }

            }
            curPage++;
        }


    }


}
